package chapter07

/**
 *
 * @author cdhuangchao3
 * @date 2023/6/11 10:56 AM
 */
object Test14_HighLevelFunc_Map {
  def main(args: Array[String]): Unit = {
    val list = List(1, 2, 3, 4, 5, 6, 77, 9)
    // 1.过滤
    println(list.filter(_ % 2 == 0))

    // 2.map
    println(list.map(_ * 2))
    println(list.map(e => e * e))

    // 3.扁平化
    val nestedList = List(List(1, 2, 3), List(4, 5), List(6, 7, 9))
    println(nestedList(0) ::: nestedList(1) ::: nestedList(2))
    println(nestedList.flatten)

    println("=" * 20)

    // 4.扁平映射
    println(nestedList.flatMap(e => e.iterator))
    println(nestedList.flatMap(_.iterator))

    val strings = List("hello world", "hello scala", "hello spark", "we study")
    println(strings.flatMap(e => e.split(" ")))
    println("=" * 20)

    // 5.group
    println(list.groupBy(_ % 2))
    println(list.groupBy(d => if (d % 2 == 0) "偶数" else "奇数"))


  }
}
